package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    //注入持久层
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByUsername(String username) {
        //查询对应的user对象
        User user=userMapper.findUserByUsername(username);

        //使用userId查询对应的角色集合
        Set<Role> roleset=userMapper.findRolesByUserId(user.getId());

        //遍历角色集合
        for (Role role : roleset) {
            //使用角色ID查询对应的权限集合
            Set<Permission> permissionSet=userMapper.findPermissionByRoleId(role.getId());

            //将权限集合赋值给到当前角色
            role.setPermissions(permissionSet);
        }

        //将角色集合赋值给当前的User对象
        user.setRoles(roleset);

        return user;
    }
}
